package com.dhcc.DRGsHosp.facade.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.openkoala.koala.commons.InvokeResult;
import org.openkoala.security.shiro.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DrgsIndexCalApplication;
import com.dhcc.DRGsHosp.application.SystemConfigApplication;
import com.dhcc.DRGsHosp.facade.SystemConfigFacade;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;

/**
 * @title		:SystemConfigFacadeImpl.java
 * @description	:对drgs.T_CONFIG增删改查FacadeImpl层
 * @author		:gehui
 * @date		:2016年11月30日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 */
@Named("drgsHospFacadeImpl44")
public class SystemConfigFacadeImpl implements SystemConfigFacade{
	@Autowired
	private SystemConfigApplication systemConfigApplication;
	
	@Autowired
	private DrgsIndexCalApplication drgsIndexCalApplication;
	
	//全表查询配置表T_CONFIG
	@Override
	public InvokeResult systemConfig(){
		List<Map<String,Object>> resultList=systemConfigApplication.systemConfig();
		return InvokeResult.success(resultList);
	}

	//按字典项代码查询字典表MR_DICTIONARYDATA
	@Override
	public InvokeResult dicSearch(String param){
		List<Object[]> resultList=systemConfigApplication.dicSearch(param);
		List<Map<String,Object>> list=new LinkedList<>();
		for(Object[] objs:resultList){
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("dicCode",objs[0]);
			map.put("dicValue", objs[1]);
			map.put("dicDesc", objs[2]);
			list.add(map);
		}
		return InvokeResult.success(list);
	}
	
	//添加/更新配置项
	@Override
	public InvokeResult saveCfg(Map<String,Object> json){
		try{
			systemConfigApplication.saveCfg(json) ;
		}catch(Exception e){
			e.printStackTrace();
			return InvokeResult.failure(e.getMessage());
		}
		return InvokeResult.success();
	}
	
	//删除配置项
	@Override 
	public InvokeResult delCfg(String param){
		try{
			systemConfigApplication.delCfg(param);
		}catch(Exception e){
			return InvokeResult.failure(e.getMessage());
		}
		return InvokeResult.success();
	}

	@Override
	public Map<Object, Object> getConfigValue() {
		List<Object[]> resultList=systemConfigApplication.getConfigValue();
		Map<Object,Object> result =new HashMap<Object,Object>();
		for(Object[] objs: resultList){
			result.put(objs[0],objs[1]);
		}
		return result;
	}
	
/**
 * 
 *  @Description：获取字段和描述
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#findTableFiles(java.lang.String)
 */
	@Override
	public InvokeResult findTableFiles(String tableName) {
		return InvokeResult.success(systemConfigApplication.findTableFiles(tableName));
	}
/**
 * 
 *  @Description：获取系统最大分组时间
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getMaxGroupYear()
 */
@Override
public Object getMaxGroupYear() {
	return systemConfigApplication.getMaxGroupYear();
}

/**
 * 
 * TODO 获取可配置的医院指标.
 * 
 * @author ShaoZhiYuan  
 * @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getHospQuota()
 */
@Override
public InvokeResult getHospQuota() {
	return InvokeResult.success(systemConfigApplication.getHospQuota()) ;
}
/**
 * 更改可配置的医院指标
 * TODO 描述该方法的实现功能（可选）.
 * 
 * @author ShaoZhiYuan  
 * @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#updateHospQuota()
 */
@Override
public void updateHospQuota(String dataHospQuote,String flag) {
	systemConfigApplication.updateHospQuota(dataHospQuote,flag);
}
/**
 * 
 *  @Description：获取年度标杆
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getStdYear()
 */
@Override
public Object getStdYear() {
	return systemConfigApplication.getStdYear();
}

@Override
public Map<String,Object> getStdInfo() {
	Map<String,Object> stdInfo = systemConfigApplication.getStdInfo();
	return stdInfo;
}
/**
 * 
 *  @Description：获取调整后的次均费用
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getStdAvgFee(java.lang.String)
 */
@Override
public Object getStdAvgFee(String stdId,String adjust) {
	return systemConfigApplication.getStdAvgFee(stdId,adjust);
}
/**
 * 
 *  @Description：获取次均费用调整参数
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getAvgFeeConfig()
 */
@Override
public Object getAvgFeeConfig() {
	return systemConfigApplication.getAvgFeeConfig();
}
/**
 * 
 *  @Description：更新调整后的次均费用
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#saveAvgFeeAdjust(java.lang.String, java.lang.String)
 */
@Override
public Object saveAvgFeeAdjust(String stdId, String adjust) {
	return systemConfigApplication.saveAvgFeeAdjust(stdId,adjust);
}
	/**
	 * 
	 *  @Description：获取医嘱开单科室
	 *  @Author WENYI  
	 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getOrderDep()
	 */
	@Override
	public Object getOrderDep() {
		// TODO Auto-generated method stub
		return systemConfigApplication.getOrderDep();
	}
	/**
	 * 
	 *  @Description：保存特殊部门配置
	 *  @Author WENYI  
	 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#saveSpecialDep(java.lang.String)
	 */
	@Override
	public int saveSpecialDep(String deps) {
		// TODO Auto-generated method stub
		return systemConfigApplication.saveSpecialDep(deps);
	}

/**
 * 
 *  @Description：获取医师显示情况
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.CaseGlobalSearchFacade#getDoctorConfig()
 */
	@Override
	public Map<String,Object> getDoctorConfig() {
		return systemConfigApplication.getDoctorConfig();
	}
/**
 * 
 *  @Description：获取医保类型有哪些
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getMedisType()
 */
@Override
public String getMedisType() {
	List<String> list =systemConfigApplication.getMedisType();
	String code="";
	if(!list.isEmpty()) {
		for(String str:list) {
			code+=str+"&";
		}
	}
	if(code.lastIndexOf("&")>0) {
		code=code.substring(0, code.length()-1);
	}
	return code;
}
/**
 * 
 *  @Description：保存用户分组方案
 *  @Author lhj 
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#setSystemGroupScheme(java.lang.String, java.lang.String, java.lang.String)
 */
@Override
public Object setSystemGroupScheme(String userId, String roleId, String groupScheme) {
	return systemConfigApplication.setSystemGroupScheme(userId, roleId, groupScheme);
}
/**
 * 
 *  @Description：获取用户配置好的分组方案
 *  @Author lhj 
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getSystemGroupScheme(java.lang.String, java.lang.String)
 */
@Override
public String getSystemGroupScheme() {
	String userId =CurrentUser.getUserAccount();
	String roleId = DrgsIndex.getRoleCode();
	String mouldeId="groupScheme_"+userId+"_"+roleId;
	return systemConfigApplication.getSystemGroupScheme(mouldeId,userId, roleId);
}
/**
 * 
 *  @Description：获取当前数据含有的分组方案
 *  @Author liuhongjun 
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getMrGroupScheme()
 */
@Override
public List<Map<String, Object>> getMrGroupScheme() {
	return systemConfigApplication.getMrGroupScheme();
}
/**
 *  @Description：获取标杆映射关系
 *  @Author：liuhongjun 
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getGroupMatchStd()
 */
@Override
public Map<String, Object> getGroupMatchStd() {
	Map<String, Object> map=new HashMap<>();
	List<Map<String, Object>> list=systemConfigApplication.getGroupStdType();
	if(!list.isEmpty()) {
		for(Map<String, Object> mapo: list) {
			String key=mapo.get("VERCODE")==null?null:mapo.get("VERCODE").toString();
			String value=mapo.get("STDTYPE")==null?null:mapo.get("STDTYPE").toString();
			if(key!=null && value!=null) {
				map.put(key, value);
			}
		}
	}
	return map;
}
/**
 *  @Description：根据分组方案获取当前分组器版本
 *  @Author：liuhongjun 
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getGroupByScheme(java.lang.String)
 */
@Override
public String getGroupByScheme(String groupScheme) {
	return systemConfigApplication.getGroupByScheme(groupScheme);
 }
/**
 *  @Description：获取分组方案
 *  @Author：liuhongjun 
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getSystemGroupScheme(java.lang.String, java.lang.String, java.lang.String)
 */
@Override
public String getSystemGroupScheme(String mouldeId, String userCode, String roleCode) {
	return systemConfigApplication.getSystemGroupScheme(mouldeId,userCode, roleCode);
}
/**
 *  @Description：在线分组获取分组器版本号
 *  @Author：liuhongjun 
 *  @see com.dhcc.DRGsHosp.facade.SystemConfigFacade#getOnlineGroup()
 */
@Override
public Object getOnlineGroup() {
	return systemConfigApplication.getOnlineGroup();
	}

@Override
public InvokeResult syncDepartment(String depInfo) {
	try{
		String deps = "";
		String[] dep1 = depInfo.split("&");
		if(dep1.length>0){
			for(String dep2: dep1){
				String[] dep3 = dep2.split(":");
				String dephis = dep3[0];
				String dep = dep3[1];
				deps += dep+",";
				String sql = "update drgs.t_department t set t.dep_his2='"+dephis+"' where instr('"+dep+"',t.dep_code)>0";
				drgsIndexCalApplication.excuteUpdate(sql, null);
			}
			String sql = "update drgs.t_department t set t.dep_his2=null where instr('"+deps+"', t.dep_code)<=0";
			drgsIndexCalApplication.excuteUpdate(sql, null);
		}
	}catch(Exception e){
		e.printStackTrace();
		return InvokeResult.failure(e.toString());
	}
	return InvokeResult.success();
}
}
